import java.util.Scanner;

/**
 *  十六进制转换八进制
 */
public class HexadecimaltoOctal {
    public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            String a[] = new String[n+1];
            for (int i = 0 ; i < n ; i++){
                a[i] = sc.next();
            }

            for (int i = 0 ; i < n ; i++){
                long b = Long.valueOf(a[i],16);
                System.out.println(b);
            }
    }
}
